package cn.vendsystem.com.mapper;

import cn.vendcommon.com.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select("select * from user where username = #{username}")
    User findByUserName(String username);

    @Insert("insert into user(phone, password, create_time, update_time, role, account) " +
            " values(#{phone},#{password},now(),now(),1,0.0) ")
    void add(String phone, String password);

    @Update("update user set nickname = #{nickname}, email = #{email} , update_time = #{updateTime} where id = #{id}")
    void update(User user);

    @Update("update user set user_pic = #{avatarUrl} , update_time = now() where id = #{id}")
    void updateAvatar(String avatarUrl , Integer id);

    @Update("update user set password = #{password} , update_time = now() where id = #{id}")
    void updatePwd(String password, Integer id);

    @Select("select * from user where id = #{userId}")
    User findByUserId(Long userId);

    @Update("update user set nickname = #{nickname}, sex = #{sex}, birthday = #{birthday}, update_time = #{updateTime} where id = #{id}")
    void updateBaseInfo(User user);
    /**
     * 根据用户id查询用户头像
     * @param userId
     * @return
     */
    @Select("select user_pic from user where id = #{userId}")
    String getAvatar(Integer userId);

    @Select("SELECT * FROM user")
    List<User> findAll();

    @Insert("INSERT INTO user(username, nickname, email, phone, sex, account, role, status, user_pic, password) " +
            "VALUES(#{username}, #{nickname}, #{email}, #{phone}, #{sex}, #{account}, #{role}, #{status}, #{userPic}, #{password})")
    int insert(User user);

    @Delete("DELETE FROM user WHERE id=#{id}")
    void delete(Integer id);

    @Update("UPDATE user SET status=#{status} WHERE id=#{id}")
    void updateStatus(Integer id, Integer status);

    @Select("select * from user where phone = #{phone}")
    User findByPhone(String phone);

    @Update("update user set login_ip = #{ip} where id = #{id}")
    void updateLoginIP(String ip, Integer id);

    @Select("select user_pic from user where id = #{userId}")
    String findUserPicByUserId(Long userId);

    @Update("update user set account = #{account} where id = #{userId}")
    void updateAccountById(Long userId, Double account);

    @Select("select role from user where id = #{userId}")
    int getRoleById(Long userId);

    @Select("select role from user where phone = #{phone}")
    Integer getRoleByPhone(String phone);
}
